package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.domain.Favorite;
import com.example.domain.Movie;
import com.example.domain.model.MovieDTO;
import com.example.domain.model.MyPage;
import com.example.domain.model.SearchDTO;
import com.example.service.FavoriteService;
import com.example.shiro.AccountProfile;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Api(tags = "收藏管理")
@RestController
@RequestMapping("/favorite")
@RequiresAuthentication
public class FavoriteController {
    @Autowired
    private FavoriteService favoriteService;

    @ApiOperation(value = "收藏列表")
    @PostMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页索引", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页显示记录数", defaultValue = "20")
    })
    public R<IPage<MovieDTO>> getList(@RequestBody SearchDTO searchDTO){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        Page page = new Page(searchDTO.getCurrent(), searchDTO.getSize());
        return R.success(favoriteService.getList(account.getId(),page,searchDTO));
    }

    @ApiOperation(value = "查看收藏")
    @GetMapping("/{movieId}")
    public R<Favorite> getInfo(@PathVariable String movieId){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        Favorite Favorite = favoriteService.getById(movieId,account.getId());
        return R.success(Favorite);
    }

    @ApiOperation(value = "添加收藏")
    @PostMapping("/{movieId}")
    public R add(@PathVariable String movieId){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();
        return favoriteService.save(movieId,account.getId())?R.success():R.error();
    }

//    @PutMapping("/update/{id}")
    public R update(@PathVariable String id,@RequestBody Favorite Favorite){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        return favoriteService.update(id,Favorite)?R.success():R.error();
    }
    @ApiOperation(value = "删除收藏")
    @DeleteMapping("/{movieId}")
    public R delete(@PathVariable String movieId){
        //获取当前用户信息
        AccountProfile account = (AccountProfile) SecurityUtils.getSubject().getPrincipal();

        return favoriteService.delete(movieId,account.getId())?R.success():R.error();
    }

}
